package doc2db.interfaces.mainWindow;

import doc2db.interfaces.printer.DocumentRenderer;
import doc2db.Keey;
import doc2db.UserProp;
import doc2db.fileWorker.FileBeginer;
import doc2db.interfaces.action.DelTableAction;
import doc2db.interfaces.action.InsUpd.InsertAction;
import doc2db.interfaces.action.ShowTableAction;
import doc2db.interfaces.action.InsUpd.UpdateAction;
import doc2db.interfaces.action.ToTreangleTabAction;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JEditorPane;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.text.JTextComponent;

/**
 *
 * @author yuriy
 */
public class JEditPContentMenu {
    public String hyperLinkName = "__NULL";
    protected JEditorPane p;
    protected JEditorPane ee;

    public JEditPContentMenu (JEditorPane __p, JEditorPane e){
        p = __p;
        ee = e;
    }

    public JPopupMenu getLynkSelectPaneMenu(final /*JEditorPane*/ JTextComponent pane){
        JPopupMenu m = new JPopupMenu();

        Action cp = new AbstractAction("Копировать") {

            @Override
            public void actionPerformed(ActionEvent e) {
                pane.copy();
            }
        };
        JMenuItem mi = new JMenuItem(cp);
        mi.setFont(UserProp.menuFont);
        m.add(mi);

        JMenuItem select = new JMenuItem(new AbstractAction("Очистить") {
            @Override
            public void actionPerformed(ActionEvent e) {
                pane.setText("");
            }
        });
        select.setFont(UserProp.menuFont);
        m.add(select);

        JMenuItem print = new JMenuItem(new AbstractAction("Печять",
                new  ImageIcon(FileBeginer.resurse_resurseFolderMenu+Keey.sp+"Printer.png")) {
            @Override
            public void actionPerformed(ActionEvent e) {
                DocumentRenderer docRend = new DocumentRenderer();
                docRend.print((JEditorPane) pane);
            }
        });
        print.setFont(UserProp.menuFont);
        m.add(print);

        return m;
    }

    public JPopupMenu getLynkMenu(){
        JPopupMenu m = new JPopupMenu();
        String str = FileBeginer.resurse_resurseFolderMenu+Keey.sp;

        JMenuItem select = new JMenuItem(new AbstractAction("Показать",
                new ImageIcon(str + "tabl.png")) {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!hyperLinkName.equals("__NULL")) {
                    ShowTableAction s = new ShowTableAction(p, ee);
                    //s.res = "" + hyperLinkName;
                    int w = Integer.parseInt(hyperLinkName.split(":")[0]);
                    int h = Integer.parseInt(hyperLinkName.split(":")[1]);
                    s.res = "" + ((MyJEditorPane)p).table[w][h];
                    s.actionPerformed(null);
                }
                hyperLinkName = "__NULL";
            }
        });
        select.setFont(UserProp.menuFont);
        m.add(select);

        JMenuItem drop = new JMenuItem(new AbstractAction("Удалить",
                new ImageIcon(str + "edit-delete_003.png")) {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!hyperLinkName.equals("__NULL")) {
                    DelTableAction s = new DelTableAction(p, ee);
                    //s.res = "" + hyperLinkName;
                    int w = Integer.parseInt(hyperLinkName.split(":")[0]);
                    int h = Integer.parseInt(hyperLinkName.split(":")[1]);
                    s.res = "" + ((MyJEditorPane)p).table[w][h];
                    s.actionPerformed(null);
                }
                hyperLinkName = "__NULL";
            }
        });
        drop.setFont(UserProp.menuFont);
        m.add(drop);

        JMenuItem insert = new JMenuItem(new AbstractAction("Дополнить",
                new ImageIcon(str + "insert.png")) {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!hyperLinkName.equals("__NULL")) {
                    InsertAction s = new InsertAction(p, ee);
                    //s.res = "" + hyperLinkName;
                    int w = Integer.parseInt(hyperLinkName.split(":")[0]);
                    int h = Integer.parseInt(hyperLinkName.split(":")[1]);
                    s.res = "" + ((MyJEditorPane)p).table[w][h];
                    s.actionPerformed(null);
                }
                hyperLinkName = "__NULL";
            }
        });
        insert.setFont(UserProp.menuFont);
        m.add(insert);

        JMenuItem update = new JMenuItem(new AbstractAction("Обновить",
                new ImageIcon(str + "update.png")) {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!hyperLinkName.equals("__NULL")) {
                    UpdateAction s = new UpdateAction(p, ee);
                    //s.res = "" + hyperLinkName;
                    int w = Integer.parseInt(hyperLinkName.split(":")[0]);
                    int h = Integer.parseInt(hyperLinkName.split(":")[1]);
                    s.res = "" + ((MyJEditorPane)p).table[w][h];
                    s.actionPerformed(null);
                }
                hyperLinkName = "__NULL";
            }
        });
        update.setFont(UserProp.menuFont);
        m.add(update);

        JMenuItem modetab = new JMenuItem(new AbstractAction("Преобразовать",
                new ImageIcon(str + "modetab.png")) {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!hyperLinkName.equals("__NULL")) {
                    ToTreangleTabAction s = new ToTreangleTabAction(p, ee);
                    int w = Integer.parseInt(hyperLinkName.split(":")[0]);
                    int h = Integer.parseInt(hyperLinkName.split(":")[1]);
                    s.res = "" + ((MyJEditorPane)p).table[w][h];
                    s.actionPerformed(null);
                }
                hyperLinkName = "__NULL";
            }
        });
        modetab.setFont(UserProp.menuFont);
        m.add(modetab);

        return m;
    }

}
